﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using System.Windows.Forms;
using PaliTextReader2.ContentsDSTableAdapters;
using PaliTextReader2.Utilities;
using System.Text.RegularExpressions;
using Lucene.Net.Analysis;

namespace PaliTextReader2.Utilities
{
    public class FileIndexer
    {
        public static bool CanonIndexExists
        {
            get
            {
                return IndexReader.IndexExists(Utility.CanonIndexDirectory);
            }
        }

        public static bool PEDIndexExists
        {
            get
            {
                return IndexReader.IndexExists(Utility.PEDIndexDirectory);
            }
        }

        /*
        public static void IndexCanon()
        {
            IndexWriter writer = new IndexWriter(Utility.CanonIndexDirectory, new SimpleAnalyzer(), true);
            writer.SetUseCompoundFile(true);
            writer.SetMaxFieldLength(0x7fffffff);

            FilesTableAdapter objFilesTA = new FilesTableAdapter();
            ContentsDS.FilesDataTable objFilesDT=objFilesTA.GetData();
            foreach (ContentsDS.FilesRow objRow in objFilesDT.Rows)
            {
                string str = Utility.GetBookContent(Utility.CanonDirectory, objRow.FileName);

                Document doc = new Document();
                doc.Add(new Field("text", GetPlainText(str), Field.Store.NO, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
                doc.Add(new Field("file", objRow.FileID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                writer.AddDocument(doc);
            }

            writer.Close();
            writer.Optimize();
        }

        public static void IndexPED()
        {
            IndexWriter writer = new IndexWriter(Utility.PEDIndexDirectory, new SimpleAnalyzer(), true);
            writer.SetUseCompoundFile(true);
            writer.SetMaxFieldLength(0x7fffffff);

            for (int i = 1; i < 38; i++)
            {
                string str = Utility.GetBookContent(Utility.PEDDirectory, i.ToString("00"));

                Document doc = new Document();
                doc.Add(new Field("text", GetPlainText(str), Field.Store.NO, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
                string strLetter=Regex.Match(str, "<span class=letter><b>([^<]+)</b></span>").Groups[1].Value;
                doc.Add(new Field("letter", strLetter, Field.Store.YES, Field.Index.NO));
                doc.Add(new Field("ID", i.ToString("00"), Field.Store.YES, Field.Index.NO));
                writer.AddDocument(doc);
            }
            
            writer.Close();
            writer.Optimize();
        }
        */

        public static string GetPlainText(string strHTML)
        {
            // also, the newline character is giving me problems, get rid of it
            string pattern = "\n";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, "");

            // ok get rid of all 

            // 1.) comments
            // xml html tag <[^>]+>
            pattern = "<![^<]*>";
            regex = new Regex(pattern);
            strHTML = regex.Replace(strHTML, "");


            // 1.) @nbsp; &amp; &gt; etc 
            pattern = "(?:&([a-z]|[A-Z])+;)"; //"@([a-z]|[A-Z])\\d{4};";
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, " ");


            // 2.) script tags
            pattern = "<script.+?</script>";
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, "");


            // 3.) head 
            pattern = "<head.+</head>";
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, "");

            // 4.) Any opening tag
            pattern = "<([a-z]|[A-Z])+[^<]*>";
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, " ");

            // 5.) And any closing tag
            pattern = "</([a-z]|[A-Z])+[^<]*>";
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
            strHTML = regex.Replace(strHTML, " ");

            //since we have replaced tags with whitespace, remvoe
            //any extra whitespace char
            pattern = @"\s{2,}";
            regex = new Regex(pattern);
            strHTML = regex.Replace(strHTML, " ");

            return strHTML;
        }
    }
}
